package email

import (
	"cqsx/database"
	"crypto/md5"
	"fmt"
	"math/rand"
	"net/smtp"
	"time"
)

func SendEmail() {
	auth := smtp.PlainAuth("", "qiao20031116@qq.com", "ujlhzazsmbnrhjaa", "smtp.qq.com")
	body := []byte("From:qiao20031116@qq.com\nTo:1181744398@qq.com\nSubject:你好dafgasd\n大幅度发")
	smtp.SendMail("smtp.qq.com:587", auth, "qiao20031116@qq.com", []string{"1700837171@qq.com"}, body)
}

func SuiJiMima(changdu int) (mingwen string, miwen string) {
	var allChar string = "ABCDEFGabc&*%$#@@!#$*%$@@#defghngkhhdf2346549780"
	var mima string

	r := rand.New(rand.NewSource(time.Now().UnixMicro()))
	for i := 0; i < changdu; i++ {
		current := r.Intn(len(allChar) - 1)
		zimu := allChar[current]
		mima := mima + string(zimu)

	}
	miwenBytes := md5.Sum([]byte(mima))
	return mima, fmt.Sprintf("%x", miwenBytes)
}
func ResetPswd() {
	var sn string
	fmt.Println("请输入你要充值密码的人的学号")
	fmt.Scanln(&sn)
	var student classmate.Classmate
	database.DBDEmo.Raw("select*from classmates.classmates where sn=?", sn).Scan(&student)
	mingwen, miwen := SuiJiMima(6)
	student.Pswd = miwen
	database.DBDEmo.Save(student)
	SendEmail([]string{student.Email}, "3409009285@qq.com", "你的新密码重置成功，请查收")
	fmt.Println("你的新密码是：", mingwen)
}
